//
//  ZZBottomView.m
//  NetesaeLottery
//
//  Created by 周英培 on 16/8/1.
//  Copyright © 2016年 周英培. All rights reserved.
//

#import "ZZBottomView.h"
#import "ZZBottomBtn.h"

@interface ZZBottomView ()

@property (nonatomic,weak) ZZBottomBtn *selecBtn;

@end

@implementation ZZBottomView

- (void)creatBtnWithImgName:(NSString *)imgName selImgName:(NSString *)selImgName {

    ZZBottomBtn *btn = [ZZBottomBtn buttonWithType:UIButtonTypeCustom];
    
    [btn setBackgroundImage:[UIImage imageNamed:imgName] forState:UIControlStateNormal];
    
    [btn setBackgroundImage:[UIImage imageNamed:selImgName] forState:UIControlStateSelected];
    
    [self addSubview:btn];
    
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchDown];
    
}

- (void)btnClick:(ZZBottomBtn *)sender {

    self.selecBtn.selected = NO;
    
    sender.selected = YES;
    
    self.selecBtn = sender;
    
    if ([self.delegate respondsToSelector:@selector(bottomView:didSelectIndex:)]) {
        
        [self.delegate bottomView:self didSelectIndex:sender.tag];
        
    }

}

- (void)layoutSubviews {

    [super layoutSubviews];
    
    CGFloat width = self.width / self.subviews.count;
    
    CGFloat height = self.height;
    
    CGFloat y = 0;
    
    [self.subviews enumerateObjectsUsingBlock:^(__kindof ZZBottomBtn * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        obj.tag = idx;
        
        CGFloat x = idx *width;
        
        obj.frame = CGRectMake(x, y, width, height);
        
        if (idx == 0) {
            
            [self btnClick:obj];
            
        }
        
    }];

}

@end
